<template>
  <div>
  <el-card shadow="never" class="border-0">
    <div class="flex items-center justify-between mb-4">
      <el-button type="primary" @click="handleNew">新增</el-button>
      <el-button type="warning" @click="export2ExcelVisible = !export2ExcelVisible">导出数据</el-button>
      <el-tooltip effect="dark" content="刷新数据" placement="top">
        <el-button text @click="refresh"
          ><el-icon :size="20"><Refresh /></el-icon
        ></el-button>
      </el-tooltip>
    </div>

    <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
      <el-table-column prop="name" label="品名" width="180"></el-table-column>
      <el-table-column prop="old_price" label="旧单价" width="180" align="right"></el-table-column>
      <el-table-column prop="new_price" label="新单价" width="180" align="right"></el-table-column>
      <el-table-column prop="img_url" label="图片连接" width="180">
        <template #default="scope">
          <img :src="scope.row.img_url" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" text @click="handleUpdate(scope.row)">修改</el-button>
          <el-popconfirm title="确定要删除吗？" confirmButtonText="确认" cancelButtonText="取消" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button text type="primary" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <form-drawer ref="formDrawerRef" :title="drawerTitle" @submit="handleSubmit">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px" :inline="false">
      <el-form-item label="品名" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="旧单价" prop="old_price">
        <el-input-number v-model="form.old_price" :min="0" :max="1000"></el-input-number>
      </el-form-item>
      <el-form-item label="新单价" prop="new_price">
        <el-input-number v-model="form.new_price" :min="0" :max="1000"></el-input-number>
      </el-form-item>
    </el-form>
  </form-drawer>

  <export-2-excel v-model='export2ExcelVisible' :records='tableData' :header-map='headerMap'></export-2-excel>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { list, insert, update } from '~/api/vue3goods'
import { errorMsg, successMsg } from '~/composables/utils'
import FormDrawer from '~/components/FormDrawer.vue'
import Export2Excel from './components/Export2Excel.vue'

const tableData = ref([])
const loading = ref(false)
const formDrawerRef = ref(null)
const drawerTitle = ref('xx')
const formRef = ref(null)
const form = reactive({
  id: 0,
  name: '',
  old_price: 0,
  new_price: 0
})

const rules = {
  name: [
    {
      required: true,
      message: '品名不可为空！',
      trigger: 'blur'
    }
  ]
}

const export2ExcelVisible = ref(false)
const headerMap = ref({
  品名: 'name',
  旧单价: 'old_price',
  新单价: 'new_price',
  入职日期: 'admission_date'
})

const getData = () => {
  loading.value = true
  list()
    .then((res) => {
      if (res.code !== 1) {
        errorMsg(res.msg)
        return
      }

      tableData.value = res.data
    })
    .finally(() => (loading.value = false))
}
getData()

const refresh = () => getData()

const handleDelete = (id) => {
  console.log('删除数据了')
}

const insertRecord = (cb) => {
  formDrawerRef.value.showLoading()
  insert(form)
    .then((res) => {
      if (res.code !== 1) {
        errorMsg(res.msg)
        return
      }
      cb()
    })
    .finally(() => formDrawerRef.value.hideLoading())
}

const updateRecord = (cb) => {
  formDrawerRef.value.showLoading()
  update(form)
    .then((res) => {
      if (res.code !== 1) {
        errorMsg(res.msg)
        return
      }
      cb()
    })
    .finally(() => formDrawerRef.value.hideLoading())
}

const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (!valid) {
      return
    }
    console.log('handleSubmit.form:', form)
    const fn = form.id > 0 ? updateRecord : insertRecord
    fn(() => {
      successMsg('保存成功！')
      formDrawerRef.value.close()
      getData()
    })
  })
}
const handleNew = () => {
  drawerTitle.value = '新增'
  form.id = 0
  form.name = ''
  form.old_price = 0
  form.new_price = 0
  formDrawerRef.value.open()
}
const handleUpdate = (row) => {
  drawerTitle.value = '修改'
  form.id = row.id
  form.name = row.name
  form.old_price = row.old_price
  form.new_price = row.new_price
  formDrawerRef.value.open()
}
</script>
<style lang="scss" scoped></style>
